using SeataNet.Core.Common;
using SeataNet.Core.Protocol;
using SeataNet.Core.Model;

namespace SeataNet.Core.Transaction
{
    /// <summary>
    /// 事务管理器接口
    /// </summary>
    public interface ITransactionManager
    {
        /// <summary>
        /// 开始全局事务
        /// </summary>
        /// <param name="transactionName">事务名称</param>
        /// <param name="timeout">超时时间（毫秒）</param>
        /// <returns>全局事务ID</returns>
        string Begin(string? transactionName = null, int timeout = 60000);

        /// <summary>
        /// 提交全局事务
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <returns>提交结果</returns>
        GlobalCommitResponse Commit(string xid);

        /// <summary>
        /// 回滚全局事务
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <returns>回滚结果</returns>
        GlobalRollbackResponse Rollback(string xid);

        /// <summary>
        /// 获取全局事务状态
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <returns>事务状态</returns>
        GlobalStatus GetStatus(string xid);

        /// <summary>
        /// 获取全局事务报告
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <returns>事务报告</returns>
        GlobalReportResponse GetGlobalReport(string xid);
        
        /// <summary>
        /// 开始全局事务（异步）
        /// </summary>
        /// <param name="timeout">超时时间（毫秒）</param>
        /// <param name="name">事务名称</param>
        /// <returns>全局事务ID</returns>
        Task<string> BeginAsync(int timeout = 60000, string? name = null);
        
        /// <summary>
        /// 提交全局事务（异步）
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <returns>提交结果</returns>
        Task<bool> CommitAsync(string xid);
        
        /// <summary>
        /// 回滚全局事务（异步）
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <returns>回滚结果</returns>
        Task<bool> RollbackAsync(string xid);
        
        /// <summary>
        /// 获取事务状态（异步）
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <returns>事务状态</returns>
        Task<int> GetStatusAsync(string xid);
        
        /// <summary>
        /// 获取全局事务上下文（异步）
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <returns>全局事务上下文</returns>
        Task<GlobalTransactionContext?> GetGlobalTransactionAsync(string xid);

        /// <summary>
        /// 注册分支事务（异步）
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <param name="resourceId">资源ID</param>
        /// <param name="lockKey">锁键</param>
        /// <returns>分支事务ID</returns>
        Task<long> RegisterBranchAsync(string xid, string resourceId, string lockKey);

        /// <summary>
        /// 上报分支事务状态（异步）
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <param name="branchId">分支事务ID</param>
        /// <param name="status">分支状态</param>
        /// <returns>上报结果</returns>
        Task ReportBranchStatusAsync(string xid, long branchId, BranchStatus status);

        /// <summary>
        /// 获取分支事务状态（异步）
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <param name="branchId">分支事务ID</param>
        /// <returns>分支状态</returns>
        Task<BranchStatus> GetBranchStatusAsync(string xid, long branchId);
    }
}